package 有序数组去重后长度;

/**
 * 有序数组去重后长度，空间复杂度O(1)
 */
public class Solution {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 3, 4, 4, 5, 5, 5,};
        int len = twoPointer(arr);
        System.out.println("len = " + len);
    }

    /**
     * 双指针法
     */
    public static int twoPointer(int[] arr) {

        int x = 0;
        for (int y = 1; y < arr.length; y++) {
            if (arr[x] != arr[y]) {
                x++;
                arr[x] = arr[y];
            }
        }

        return x + 1;
    }

}
